import {ethers} from "ethers"

const oneGwei = ethers.getBigInt("1000000000")
console.log(oneGwei)
console.log(ethers.getBigInt("0x3b9aca00"))
console.log(ethers.getBigInt(1000000000))
// 超过js最大安全整数的数值将不能转换
console.log("js中最大安全整数：", Number.MAX_SAFE_INTEGER)
// 下边代码会报错
// console.log(ethers.getBigInt(Number.MAX_SAFE_INTEGER+1))
console.log(oneGwei == 1000000000)
console.log(oneGwei == 1000000000n)

// wei gwei=10^9wei ether=10^9gwei=10^18wei 为常用的数值单位
console.group('\n1. 格式化：小单位转大单位，formatUnits');
console.log(ethers.formatUnits(oneGwei, 0));
// '1000000000'
console.log(ethers.formatUnits(oneGwei, "gwei"));
// '1.0'
console.log(ethers.formatUnits(oneGwei, 9));
// '1.0'
console.log(ethers.formatUnits(oneGwei, "ether"));
// `0.000000001`
console.log(ethers.formatUnits(1000000000, "gwei"));
// '1.0'
console.log(ethers.formatEther(oneGwei));
// `0.000000001` 等同于formatUnits(value, "ether")
console.groupEnd();

console.group('\n2. 解析：大单位转小单位，parseUnits');
console.log(ethers.parseUnits("1.0").toString());
// { BigNumber: "1000000000000000000" }
console.log(ethers.parseUnits("1.0", "ether").toString());
// { BigNumber: "1000000000000000000" }
console.log(ethers.parseUnits("1.0", 18).toString());
// { BigNumber: "1000000000000000000" }
console.log(ethers.parseUnits("1.0", "gwei").toString());
// { BigNumber: "1000000000" }
console.log(ethers.parseUnits("1.0", 9).toString());
// { BigNumber: "1000000000" }
console.log(ethers.parseEther("1.0").toString());
// { BigNumber: "1000000000000000000" } 等同于parseUnits(value, "ether")
console.groupEnd();